from django.db import models
from django.contrib.auth.models import User
import json
#from helpers.director.model_func.cus_fields.multichoice import MultiChoiceField
# Create your models here.

class DepartMent(models.Model):
    name = models.CharField('部门名称',max_length =200)
    #leader = models.CharField('领导',max_length=100,blank=True)
    leader = models.ManyToManyField(to=User,verbose_name='负责人',related_name='department_leader',blank=True)
    parent = models.ForeignKey('DepartMent',verbose_name = '上级部门',related_name='children',null=True,blank=True)
    path = models.CharField('路径',max_length=100,blank=True)
    
    def __str__(self):
        return self.name
    
class Userinfo(models.Model):
    user = models.OneToOneField(to=User)
    name = models.CharField('姓名',max_length=100,blank=True)
    department = models.ForeignKey(DepartMent,null=True,blank=True,verbose_name='部门')
    #head = models.CharField('头像',max_length=300,blank=True)

class ProductPart(models.Model):
    name = models.CharField('部件名称',max_length=200)
    def __str__(self):
        return self.name    

class FailSource(models.Model):
    name = models.CharField('来源名称',max_length=200)
    def __str__(self):
        return self.name    
    
class FailKind(models.Model):
    name = models.CharField('种类名称',max_length=200)
    def __str__(self):
        return self.name    

class ProcessingScheme(models.Model):
    name = models.CharField('方案名称',max_length=200)
    def __str__(self):
        return self.name  

PRIORITY=(
    (1,'低'),
    (2,'中'),
    (3,'高'),
)
EVENT_STATUS=(
    #(1,'提交'),
    (2,'处理中'),
    (3,'结件'),
    (4,'同意结件'),
    (5,'关闭'),
    (6,'总经办审批'),
)

EVENT_KIND = (
    (1,'过程不合格'),
    (5,'成品不合格'),
    (2,'客诉'),
    (3,'供应商'),
    (4,'其他'),
    
    
)
class QualityEvent(models.Model):
    title = models.CharField('标题',max_length=200,blank=True)
    kind = models.IntegerField('类别',choices=EVENT_KIND,default=1)
    
    prod_record = models.ForeignKey(to="ProdRecord",null=True,blank=True,verbose_name='关联工单')
    prod_no = models.CharField('工单号',max_length=200,blank=True) # '生产批号' 修改为工单号
    model_no = models.CharField('品号',max_length=50,blank=True)
    model_name = models.CharField('品名',max_length=100,blank=True)
    model_spec = models.CharField('规格',max_length=100,blank=True)
    
    
    #prod_model = models.CharField('产品型号',max_length=200)
    fail_count = models.IntegerField('不合格数量',default=0,)
    total_count = models.IntegerField('总数',default=0)
    fail_ratio = models.FloatField('不合格率',default=0,blank=True)
    #prod_part = models.ForeignKey(ProductPart,verbose_name='产品部件')
    
    fail_source = models.ForeignKey(FailSource,verbose_name='不合格来源')
    fail_kind = models.ForeignKey(FailKind,verbose_name='不合格种类',null=True,blank=True)
    priority = models.IntegerField('优先级',choices=PRIORITY,default=1)
    status = models.IntegerField('状态',choices=EVENT_STATUS,default=2)
    fail_memo = models.TextField('不合格描述',blank=True)
    
    fail_content = models.TextField('不合格内容',blank=True) # 第二次增加的
    
    create_user = models.ForeignKey(to=User,verbose_name='创建人',blank=True,null=True)
    create_time = models.DateTimeField('创建时间',auto_now_add=True)
    create_depart = models.ForeignKey(to=DepartMent,blank=True,null=True,verbose_name='发起部门',related_name='created_event')
    
    processing_scheme = models.ForeignKey(ProcessingScheme,verbose_name='处理方案',blank=True,null=True)
    process_worker = models.ForeignKey(User,verbose_name='处理人',blank=True,null=True,related_name='process_event')
    process_datetime = models.DateTimeField(verbose_name='处理日期',blank=True,null=True,)
    tech_suggest = models.TextField('技术意见',blank=True)
    craft_suggest = models.TextField('工艺意见',blank=True)
    tech_correct_action = models.TextField('技术纠正和预防措施',blank=True)
    craft_correct_action = models.TextField('工艺纠正和预防措施',blank=True)
    
    validate_worker = models.ForeignKey(to=User,verbose_name='质量验证员',blank=True,null=True,related_name='validate_event')
    validate_datetime = models.DateTimeField(verbose_name='验证日期',blank=True,null=True)
    validate_action = models.TextField('验证措施',blank=True)
    
    approval_worker = models.ForeignKey(to=User,verbose_name='审批人员',blank=True,null=True,related_name='approval_event')
    approval_datetime = models.DateTimeField(verbose_name='审批日期',blank=True,null=True)
    approval_suggest = models.TextField('审批意见',blank=True)
    
    current_user = models.ForeignKey(User,verbose_name = '当前员工',blank=True,null=True,related_name='current_event')
    current_depart = models.ForeignKey(to=DepartMent,blank=True,null=True,verbose_name='当前部门',)
    
    customer_info = models.CharField('客户信息',max_length=100,blank=True)
    quit_number =  models.CharField('退单号',max_length=100,blank=True)
    
    supplyer_info = models.CharField('供应商信息',max_length =100,blank=True)
    receive_number = models.CharField('收料单号',max_length=100,blank=True)
    printed = models.BooleanField('已经打印',default=False)
    
    def __str__(self):
        return self.title
    
    #def save(self, **kws):
        #if self.fail_content:
            #ls = json.loads(self.fail_content)
            #count = 0
            #for ii in ls:
                #count += int( ii.get('count') )
            #self.fail_count = count
            #self.fail_ratio = self.fail_content / self.total_count
        #else:
            #self.fail_count = 0
            #self.fail_ratio =0
        #return super().save(**kws)

class Discussion(models.Model):
    user = models.ForeignKey(User,verbose_name='发送人员')
    event = models.ForeignKey(QualityEvent,verbose_name= '质量事件',)
    content = models.TextField('内容',blank=True)
    createtime = models.DateTimeField(verbose_name='创建时间',auto_now_add=True)

FLOW_LOG_KIND = (
    (1,'普通流转'),
    (2,'发起结件'),
    (3,'同意结件'),
    (4,'关闭')
)

class FlowLog(models.Model):
    user = models.ForeignKey(User,verbose_name='操作人员')
    event = models.ForeignKey(QualityEvent,verbose_name= '质量事件',)
    target = models.ForeignKey(User,verbose_name='目标人员',related_name='from_log')
    memo = models.TextField('备注',blank=True)
    kind = models.IntegerField('类型',default=1,choices=FLOW_LOG_KIND)
    createtime = models.DateTimeField(verbose_name='创建时间',auto_now_add=True)

class ProdModel(models.Model):
    no = models.CharField('品号',max_length=50, unique=True)
    name = models.CharField('品名',max_length=100,blank=True)
    specification = models.CharField('规格',max_length=200,blank=True)  
    #unit = models.CharField('单位',max_length=30,blank=True)
    designer = models.CharField('设计人',max_length=50,blank=True)
    
    def __str__(self):
        return self.name
    

class ProdRecord(models.Model):
    no = models.CharField('工单单号',max_length=50,)
    #order_number = models.CharField('工单号',max_length=100,)
    open_date = models.CharField('开单日期',blank=True,max_length=50)
    main_part_code = models.CharField('主件品号',blank=True,max_length=50)
    main_part_name = models.CharField('主件品名',blank=True,max_length=50)
    main_part_spec = models.CharField('主件规格',blank=True,max_length=50)
    main_part_unit = models.CharField('主件单位',blank=True,max_length=50)
    
    child_part_code = models.CharField('子件品号',blank=True,max_length=80)
    child_part_name = models.CharField('子件品名',blank=True,max_length=80)
    child_part_spec = models.CharField('子件规格',blank=True,max_length=80)
    child_part_unit = models.CharField('子件单位',blank=True,max_length=80)
    
    model = models.ForeignKey(to= ProdModel,null=True,blank=True,verbose_name = '生产型号')
    count = models.IntegerField('生产数量',default=0)
    
    def __str__(self):
        return self.no

class FailAbstract(models.Model):
    desp = models.CharField('简述内容',max_length=100)
    
    def __str__(self):
        return self.desp
    